package org.sbbs.app.security.webapp.action;

import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import org.sbbs.app.security.model.Menu;
import org.sbbs.app.security.service.MenuManager;
import org.sbbs.app.security.service.UrlSecurityResourceManager;
import org.sbbs.base.webapp.action.BaseJqgridEditAction;

public class MenuEditAction extends BaseJqgridEditAction {

	private Menu model;
	private MenuManager menuManager;
	private UrlSecurityResourceManager UrlSecurityResourceManager;

	public Menu getModel() {
		return model;
	}

	public void setModel(Menu model) {
		this.model = model;
	}

	public MenuManager getMenuManager() {
		return menuManager;
	}

	public void setMenuManager(MenuManager menuManager) {
		this.menuManager = menuManager;
	}

	public UrlSecurityResourceManager getUrlSecurityResourceManager() {
		return UrlSecurityResourceManager;
	}

	public void setUrlSecurityResourceManager(
			UrlSecurityResourceManager urlSecurityResourceManager) {
		UrlSecurityResourceManager = urlSecurityResourceManager;
	}

	public String edit() {
		if (getModelId() != null) {
			this.setModel(this.menuManager.get(Long
					.parseLong(this.getModelId())));
			this.setEditType(EDIT_TYPE_EDIT);
		} else {
			this.setModel(new Menu());
			this.setEditType(EDIT_TYPE_NEW);
		}
		return this.SUCCESS;

	}

	public String save() {
		try {
			HttpServletRequest req = this.getRequest();
			boolean isNew = (this.getEditType() == 1);
			this.getMenuManager().insertNode((Menu) model);
			this.setMessage(getText((isNew) ? "menu.added" : "menu.updated",
					"no msg key found,save successed."));
		} catch (Exception e) {
			this
					.setMessage(getText("error.saved", new String[]{e
							.getMessage()}));
			this.setReturnCode(RETURN_STATUS_ERROR);
		}
		return this.SUCCESS;
	}

	public String delete() {
		try {

			StringTokenizer ids = new StringTokenizer(this.getDelIds(), ",");
			while (ids.hasMoreTokens()) {
				Long removeId = Long.parseLong(ids.nextToken());
				this.getMenuManager().delete(removeId);
			}
			this.setMessage(getText("menu.deleted",
					"no msg key found,delete successed."));
		} catch (Exception e) {
			this.setMessage(getText("error.deleted", new String[]{e
					.getMessage()}));
			this.setReturnCode(RETURN_STATUS_ERROR);
		}
		return this.SUCCESS;
	}

	public String initMenuFromUrlResource() {
		try {
			List urls = this.UrlSecurityResourceManager.getAll();
			this.menuManager.initMenuFromUrlResource(urls);

			this.setMessage(getText("menu.inited",
					"no msg key found,menu inited  successed."));
		} catch (Exception e) {
			this.setMessage(getText("error.common",
					new String[]{e.getMessage()}));
			this.setReturnCode(RETURN_STATUS_ERROR);
		}
		return this.SUCCESS;
	}

	public String rebuildMenuTree() {
		this.menuManager.reBuildAllTree();
		this.setMessage("rebuild nest set tree success.");
		return this.SUCCESS;
	}
}
